package com.classic.okhttp.services;

import com.classic.okhttp.Cons;
import android.app.Activity;
import com.classic.okhttp.base.OkHttpUtils;
import com.classic.okhttp.zmoumall.utils.MD5;
import com.classic.okhttp.zmoumall.http.ACache;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.classic.okhttp.zmoumall.http.ObjectCallback;
import okhttp3.MediaType;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;

import com.classic.okhttp.beans.HVVenueBean;
import com.classic.okhttp.beans.HVVenueInfoBean;
import com.classic.okhttp.beans.HVVenueWeekTimesInfoBean;
import com.classic.okhttp.beans.HVVenueItemInfoBean;
import com.classic.okhttp.beans.HVVenueMapBean;


import com.classic.okhttp.enums.Enum;

public class IVenueService
{

	private static String _getFullURL(String method)
	{
		return new StringBuffer( Cons.GATEWAY_URL ).append("venue/").append(method).toString();
	}

	//获取场馆列表
	public static void getVenueList(Activity activity, String cityCode, Double lng, Double lat, String projectId, Integer startTime, Integer endTime, String keyword, int pageIndex, int pageSize, Enum.EnumVenueListType venueListType, String tagTitle, Boolean inWidget, boolean isCache, ObjectCallback<ArrayList<HVVenueBean>> callback)
	{
		callback.setCallBackBean(new TypeToken<ArrayList<HVVenueBean>>(){}.getType());
		callback.setContext(activity);

		Map<String, Object> params = new HashMap<>();
		params.put("cityCode", cityCode);
		params.put("lng", lng);
		params.put("lat", lat);
		params.put("projectId", projectId);
		params.put("startTime", startTime);
		params.put("endTime", endTime);
		params.put("keyword", keyword);
		params.put("pageIndex", pageIndex);
		params.put("pageSize", pageSize);
		params.put("venueListType", venueListType.value);
		params.put("tagTitle", tagTitle);
		params.put("inWidget", inWidget);

		Gson gson=new Gson();
		String json=gson.toJson(params);

		String url = _getFullURL("getVenueList");

		if(isCache) {//添加缓存
			String key = MD5.MD5Encode(new StringBuffer(url).append(cityCode).append(lng).append(lat).append(projectId).append(startTime).append(endTime).append(keyword).append(pageIndex).append(pageSize).append(venueListType).append(tagTitle).append(inWidget).toString());
			ACache aCache = ACache.get(activity);
			callback.setACache(aCache, key, isCache);
		}

		OkHttpUtils.postString()
                .mediaType(MediaType.parse("application/json;charset=utf-8"))
               	.url(url)
                .content(json)
                .addHeader("Token",Cons.mToken)
                .addHeader("DeviceID",Cons.mDeviceId)
				.addHeader("DeviceType",Cons.mDeviceType)
				.addHeader("Version",Cons.mVersion)
				.addHeader("VersionCode",Cons.mVersionCode)
				.addHeader("Data",Cons.getData(json))
				.addHeader("AppType",Cons.getAppType())
                .tag(activity)
                .build()
                .execute(callback);
	}

	//获取场馆详情
	public static void getVenueInfo(Activity activity, String venueId, boolean isCache, ObjectCallback<HVVenueInfoBean> callback)
	{
		callback.setCallBackBean(new TypeToken<HVVenueInfoBean>(){}.getType());
		callback.setContext(activity);

		Map<String, Object> params = new HashMap<>();
		params.put("venueId", venueId);

		Gson gson=new Gson();
		String json=gson.toJson(params);

		String url = _getFullURL("getVenueInfo");

		if(isCache) {//添加缓存
			String key = MD5.MD5Encode(new StringBuffer(url).append(venueId).toString());
			ACache aCache = ACache.get(activity);
			callback.setACache(aCache, key, isCache);
		}

		OkHttpUtils.postString()
                .mediaType(MediaType.parse("application/json;charset=utf-8"))
               	.url(url)
                .content(json)
                .addHeader("Token",Cons.mToken)
                .addHeader("DeviceID",Cons.mDeviceId)
				.addHeader("DeviceType",Cons.mDeviceType)
				.addHeader("Version",Cons.mVersion)
				.addHeader("VersionCode",Cons.mVersionCode)
				.addHeader("Data",Cons.getData(json))
				.addHeader("AppType",Cons.getAppType())
                .tag(activity)
                .build()
                .execute(callback);
	}

	//获取周场次信息
	public static void getVenueWeekTimesList(Activity activity, String venueId, String projectId, boolean isCache, ObjectCallback<HVVenueWeekTimesInfoBean> callback)
	{
		callback.setCallBackBean(new TypeToken<HVVenueWeekTimesInfoBean>(){}.getType());
		callback.setContext(activity);

		Map<String, Object> params = new HashMap<>();
		params.put("venueId", venueId);
		params.put("projectId", projectId);

		Gson gson=new Gson();
		String json=gson.toJson(params);

		String url = _getFullURL("getVenueWeekTimesList");

		if(isCache) {//添加缓存
			String key = MD5.MD5Encode(new StringBuffer(url).append(venueId).append(projectId).toString());
			ACache aCache = ACache.get(activity);
			callback.setACache(aCache, key, isCache);
		}

		OkHttpUtils.postString()
                .mediaType(MediaType.parse("application/json;charset=utf-8"))
               	.url(url)
                .content(json)
                .addHeader("Token",Cons.mToken)
                .addHeader("DeviceID",Cons.mDeviceId)
				.addHeader("DeviceType",Cons.mDeviceType)
				.addHeader("Version",Cons.mVersion)
				.addHeader("VersionCode",Cons.mVersionCode)
				.addHeader("Data",Cons.getData(json))
				.addHeader("AppType",Cons.getAppType())
                .tag(activity)
                .build()
                .execute(callback);
	}

	//获取场次信息
	public static void getVenueItemList(Activity activity, String venueId, String projectId, int time, boolean isCache, ObjectCallback<HVVenueItemInfoBean> callback)
	{
		callback.setCallBackBean(new TypeToken<HVVenueItemInfoBean>(){}.getType());
		callback.setContext(activity);

		Map<String, Object> params = new HashMap<>();
		params.put("venueId", venueId);
		params.put("projectId", projectId);
		params.put("time", time);

		Gson gson=new Gson();
		String json=gson.toJson(params);

		String url = _getFullURL("getVenueItemList");

		if(isCache) {//添加缓存
			String key = MD5.MD5Encode(new StringBuffer(url).append(venueId).append(projectId).append(time).toString());
			ACache aCache = ACache.get(activity);
			callback.setACache(aCache, key, isCache);
		}

		OkHttpUtils.postString()
                .mediaType(MediaType.parse("application/json;charset=utf-8"))
               	.url(url)
                .content(json)
                .addHeader("Token",Cons.mToken)
                .addHeader("DeviceID",Cons.mDeviceId)
				.addHeader("DeviceType",Cons.mDeviceType)
				.addHeader("Version",Cons.mVersion)
				.addHeader("VersionCode",Cons.mVersionCode)
				.addHeader("Data",Cons.getData(json))
				.addHeader("AppType",Cons.getAppType())
                .tag(activity)
                .build()
                .execute(callback);
	}

	//获取场馆地图信息列表
	public static void getVenueMapList(Activity activity, String cityCode, String projectId, Integer limit, boolean isCache, ObjectCallback<ArrayList<HVVenueMapBean>> callback)
	{
		callback.setCallBackBean(new TypeToken<ArrayList<HVVenueMapBean>>(){}.getType());
		callback.setContext(activity);

		Map<String, Object> params = new HashMap<>();
		params.put("cityCode", cityCode);
		params.put("projectId", projectId);
		params.put("limit", limit);

		Gson gson=new Gson();
		String json=gson.toJson(params);

		String url = _getFullURL("getVenueMapList");

		if(isCache) {//添加缓存
			String key = MD5.MD5Encode(new StringBuffer(url).append(cityCode).append(projectId).append(limit).toString());
			ACache aCache = ACache.get(activity);
			callback.setACache(aCache, key, isCache);
		}

		OkHttpUtils.postString()
                .mediaType(MediaType.parse("application/json;charset=utf-8"))
               	.url(url)
                .content(json)
                .addHeader("Token",Cons.mToken)
                .addHeader("DeviceID",Cons.mDeviceId)
				.addHeader("DeviceType",Cons.mDeviceType)
				.addHeader("Version",Cons.mVersion)
				.addHeader("VersionCode",Cons.mVersionCode)
				.addHeader("Data",Cons.getData(json))
				.addHeader("AppType",Cons.getAppType())
                .tag(activity)
                .build()
                .execute(callback);
	}
}
